/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dbmapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javabean.StoryBean;
import dbconnection.DBConnectionService;
import java.sql.Connection;
import javabean.UserBean;

/**
 *
 * @author Admin
 */
public class StoryMapper extends DBMapper {

    public ArrayList<StoryBean> LayDanhSachTruyen() throws Exception {

        ArrayList ds = new ArrayList<StoryBean>();
        Statement st = con.createStatement();
         StringBuilder sqlStr = new StringBuilder();
        sqlStr.append(" SELECT * FROM Truyen; ");
        ResultSet rs = st.executeQuery(sqlStr.toString());
        
        while (rs != null && rs.next()) {
            StoryBean stbean;
            stbean = new StoryBean();
            String ma = rs.getString("maso");
            stbean.setMa(ma.indexOf(ma));
            String theloai = rs.getString("theloai");
            stbean.setTl(theloai);
            String title = rs.getString("tieude");
            stbean.setTitle(title);
            String nn = rs.getString("ngonngu");
            stbean.setNgonngu(nn);
            String linkHinh = rs.getString("hinh");
            stbean.setImage(linkHinh);
            String gt = rs.getString("gioithieu");
            stbean.setGtt(gt);
            String giaban = rs.getString("giaban");
            stbean.setPrice(giaban);
            
            ds.add(stbean);

        }

        return ds;
    }

    public ArrayList<StoryBean> LayTruyenTheoTheLoai(String tenloai) throws ClassNotFoundException {
        ArrayList<StoryBean> ds = new ArrayList<StoryBean>();

        try {
            Statement st1 = con.createStatement();
            String sql = String.format("SELECT * FROM Truyen WHERE TheLoai=(N'%s')", tenloai);
            ResultSet rs = st1.executeQuery(sql);
            while (rs.next()) {
               StoryBean stbean;
            stbean = new StoryBean();
            String ma = rs.getString("maso");
            stbean.setMa(ma.indexOf(ma));
            String theloai = rs.getString("theloai");
            stbean.setTl(theloai);
            String title = rs.getString("tieude");
            stbean.setTitle(title);
            String nn = rs.getString("ngonngu");
            stbean.setNgonngu(nn);
            String linkHinh = rs.getString("hinh");
            stbean.setImage(linkHinh);
            String gt = rs.getString("gioithieu");
            stbean.setGtt(gt);
            String giaban = rs.getString("giaban");
            stbean.setPrice(giaban);

                ds.add(stbean);
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

        return ds;
    }
    
    public ArrayList<StoryBean> LayTruyenTheoTen(String tentruyen) throws ClassNotFoundException {
        ArrayList<StoryBean> ds = new ArrayList<StoryBean>();

        try {
            Statement st1 = con.createStatement();
            String sql = String.format("SELECT * FROM Truyen WHERE tieude=(N'%s')", tentruyen);
            ResultSet rs = st1.executeQuery(sql);
            while (rs.next()) {
               StoryBean stbean;
            stbean = new StoryBean();
            String ma = rs.getString("maso");
            stbean.setMa(ma.indexOf(ma));
            String theloai = rs.getString("theloai");
            stbean.setTl(theloai);
            String title = rs.getString("tieude");
            stbean.setTitle(title);
            String nn = rs.getString("ngonngu");
            stbean.setNgonngu(nn);
            String linkHinh = rs.getString("hinh");
            stbean.setImage(linkHinh);
            String gt = rs.getString("gioithieu");
            stbean.setGtt(gt);
            String giaban = rs.getString("giaban");
            stbean.setPrice(giaban);

                ds.add(stbean);
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

        return ds;
    }
}
